package ontocompo.fractalcomponentmanager.lib;

import java.io.File;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import ontocompo.semantic.SemanticInfo;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.objectweb.fractal.adl.ADLException;
import org.objectweb.fractal.adl.Factory;
import org.objectweb.fractal.adl.FactoryFactory;

import fr.inria.acacia.corese.api.EngineFactory;
import fr.inria.acacia.corese.api.IEngine;
import fr.inria.acacia.corese.exceptions.EngineException;

public class Resources {
	
	public static String GENERATEDFILES_DIRECTORY_PATH = "." + File.separator + "ontowebapplications" + File.separator + "generated" + File.separator;
	
	private static Factory FRACTALFACTORY = null;
	
	public static Factory getFractalFactory() throws RemoteException {		
		if(Resources.FRACTALFACTORY == null) {
			try {
				Resources.FRACTALFACTORY = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
			} catch (ADLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return Resources.FRACTALFACTORY;
	}
	
	public static HashMap<String, WebApplication> LOADED_APPS = new HashMap<String, WebApplication>();
	
	public static String ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH = "." + File.separator + "ontowebapplications" + File.separator;
	
	public static HashMap<String, WebApplication> AVAILABLE_APPS = new HashMap<String, WebApplication>();
	
	public static void buildAvailableApplications() {
		SAXBuilder sxb = new SAXBuilder();
		
		File directoryToScan = new File(ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH + "list" + File.separator);
		File[] files = directoryToScan.listFiles();
		
		for(File f : files) {
			if(f.getName().endsWith(".ontoapp")) {
				Document document = null;
				try
				{
					document = sxb.build(f);
				}
				catch(Exception e){
					e.printStackTrace();
				}
		
				Element racine = document.getRootElement();
				
				String name = racine.getChildText("name");
				String appId = racine.getChildText("appid");
				String appdir = racine.getChildText("appdir");
				String ontographname = racine.getChildText("ontographname");
				
				AVAILABLE_APPS.put(appId, new WebApplication(appId, name, new SemanticInfo(appdir, ontographname)));
			}
		}
	}
	
	public static String SCHEMAS_DIRECTORY_NAME = "semantic" + File.separator + "schemas" + File.separator;
	public static String RULES_DIRECTORY_NAME = "semantic" + File.separator + "rules" + File.separator;
	//public static String APPLICATIONS_DIRECTORY_NAME = "applications/";
	
	private static IEngine CORESEENGINE = null;
	
	public static IEngine getCoreseEngine() {
		if(Resources.CORESEENGINE == null) {
			System.out.println("Corese init");
			try {
				EngineFactory ef = new EngineFactory();
				Resources.CORESEENGINE = ef.newInstance();
				
				Resources.CORESEENGINE.load(Resources.ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH + Resources.SCHEMAS_DIRECTORY_NAME);
				Resources.CORESEENGINE.load(Resources.ONTOAPPLICATIONS_ROOT_DIRECTORY_PATH + Resources.RULES_DIRECTORY_NAME);
				
				// run rules
				Resources.CORESEENGINE.runRuleEngine();
			} catch(EngineException e) {
				e.printStackTrace();
			}
		}
		return Resources.CORESEENGINE;
	}
	
	public static void addResourceToCoreseEngine(String resource) {
		IEngine coreseEngine = Resources.getCoreseEngine();
		
		try {
			coreseEngine.load(resource);
			
			// run rules
			coreseEngine.runRuleEngine();
		} catch(EngineException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public static String join(Collection s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
}
